package com.ruangong.GitClient.HunHePanAPI;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;

public class getTabID {
    private static int total;
    public static String getData(int id, int page) {
        String apiUrl = "https://api.hunhepan.com/v1/extab/raw_disks/" + id + "?page=" + page + "&size=30";
        StringBuilder response = new StringBuilder();

        try {
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
            } else {
                response.append("Error in API call, response code: ").append(responseCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response.toString();
    }

    public static List<MovieLink> getList(int id,int page) {
        List<MovieLink> movieLinks = new ArrayList<>();

        String apiResponse = getTabID.getData(id, page);

        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(apiResponse, JsonObject.class);
        if (jsonObject == null) return null;
        JsonObject dataObject = jsonObject.getAsJsonObject("data");
        JsonElement listElement = dataObject.get("list");
        if(listElement == null ||listElement.isJsonNull()) return null;
        // 检查 dataObject 是否为 null
        if (dataObject != null) {
            total = dataObject.get("total").getAsInt();
            JsonArray listArray = dataObject.getAsJsonArray("list");

            // 检查 li
            // stArray 是否为 null
            if (listArray != null) {
                // 遍历 listArray，获取 disk_type 和 link，并存入 MovieLink 对象列表
                for (JsonElement element : listArray) {
                    JsonObject item = element.getAsJsonObject();
                    String diskType = item.get("disk_type").getAsString();
                    //System.out.println(diskType);
                    String link = item.get("link").getAsString();
                    //System.out.println(link);
                    String diskName = item.get("disk_name").getAsString();
                    //System.out.println(diskName);
                    String shareUser = item.get("share_user").getAsString();
                    //System.out.println(shareUser);
                    // 判断 is_mine 是否为 false


                    MovieLink movieLink = new MovieLink(diskType, diskName, link, shareUser);
                    movieLinks.add(movieLink);


                }
            } else {
                // 处理 listArray 为 null 的情况
                return null;
            }
        } else {
            // 处理 dataObject 为 null 的情况
            return null;
        }
        return movieLinks;
    }

    public static List<MovieLink> getResult(int id) {
        List<MovieLink> resultList = new ArrayList<>();
        if(getTabID.getList(id,1) == null) return null;
        resultList.addAll(getTabID.getList(id,1));
        for(int i = 1;i*30 < total;i++){
            resultList.addAll(getTabID.getList(id,i+1));
        }
        return resultList;
    }
    public static void main(String[] args) {
        // Example usage:
        int id = 14;
        int page = 2;
        int size = 29;
        System.out.println(getTabID.getData(id,40));
        //System.outprintln();
        List<MovieLink> movieLinks =getTabID.getResult(id);
        System.out.println("movieLinks 的长度为: " + movieLinks.size());
        // 获取 movieLinks 列表
        if(movieLinks == null) System.out.println("失败");
        else{for (MovieLink link : movieLinks) {

            System.out.println("Disk Type: " + link.getDiskType() + ", Link: " + link.getLink() + ", DiskName: " + link.getDiskName()+", DiskType " + link.getShareUser());
        }}

    }
}

